package it.polimi.travelDream.ejb.servlet;

import it.polimi.travelDream.ejb.interfaces.*;
import it.polimi.travelDream.entities.*;
import it.polimi.travelDream.entities.Package;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.lang.Object;

import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TravelServlet
 */
@WebServlet("/AddPersonalizedPackage")
public class AddPersonalizedPackage extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	@EJB
	UserMgrRemote umgr;
	
	@EJB
	PackageMgrRemote pmgr;
	
	@EJB
	PersonalizedMgrRemote ppmgr;
	
	@EJB
	TransportationMgrRemote tmgr;
	
	@EJB
	HotelMgrRemote hmgr;
	
	@EJB
	ExcursionMgrRemote emgr;
	
	@EJB
	TravelListMgrRemote tlmgr;
	
	public AddPersonalizedPackage() {
		super();
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String[]> mappa = request.getParameterMap();
		
		
		int id=Integer.parseInt(mappa.get("idUser")[0]);
		 EntityManagerFactory logfactory=Persistence.createEntityManagerFactory("it.polimi.travelDream");
 		EntityManager logem=logfactory.createEntityManager();
		User u=logem.find(User.class, id);
		 logem.close();
         logfactory.close();
		
		Package pack = new Package();
		pack = pmgr.getPackage(Integer.parseInt(mappa.get("idPackage")[0]));
		
		Transportation departure = new Transportation();
		departure = tmgr.getTransportation(Integer.parseInt(mappa.get("idDeparture")[0]));		
		
		Transportation ret = new Transportation();
		ret = tmgr.getTransportation(Integer.parseInt(mappa.get("idReturn")[0]));		
		
		Hotel hotel = new Hotel();
		hotel = hmgr.getHotel(Integer.parseInt(mappa.get("idHotel")[0]));		
		
		List<Excursion> excursionlist = new ArrayList<Excursion>();
				
	
		if (mappa.get("idExcursion0") != null)
			{
			if (mappa.get("idExcursion0")[0] != null)
			{
				Excursion excursion = new Excursion();
				excursion = emgr.getExcursion(Integer.parseInt(mappa.get("idExcursion0")[0]));
				excursionlist.add(excursion);
			}
		}		
		if (mappa.get("idExcursion1") != null)
		{
		if (mappa.get("idExcursion1")[0] != null)
		{
			Excursion excursion = new Excursion();
			excursion = emgr.getExcursion(Integer.parseInt(mappa.get("idExcursion1")[0]));
			excursionlist.add(excursion);
		}
	}	
		if (mappa.get("idExcursion2") != null)
		{
		if (mappa.get("idExcursion2")[0] != null)
		{
			Excursion excursion = new Excursion();
			excursion = emgr.getExcursion(Integer.parseInt(mappa.get("idExcursion2")[0]));
			excursionlist.add(excursion);
		}
	}	
		if (mappa.get("idExcursion3") != null)
		{
		if (mappa.get("idExcursion3")[0] != null)
		{
			Excursion excursion = new Excursion();
			excursion = emgr.getExcursion(Integer.parseInt(mappa.get("idExcursion3")[0]));
			excursionlist.add(excursion);
		}
	}	
		if (mappa.get("idExcursion4") != null)
		{
		if (mappa.get("idExcursion4")[0] != null)
		{
			Excursion excursion = new Excursion();
			excursion = emgr.getExcursion(Integer.parseInt(mappa.get("idExcursion4")[0]));
			excursionlist.add(excursion);
		}
	}	


		
			
		
		excursionlist.size();
		
		Modified_Package pp = new Modified_Package();
		pp.setName(u);
		pp.setNative_id(pack);
		pp.setDeparture(departure);
		pp.setRet(ret);
		pp.setHotel(hotel);
		pp.setExcursion(excursionlist);
		
		
		pp.getRet();
		pp.getName();
		pp.getId();
		pp.getDeparture();
		pp.getHotel();
		pp.getExcursion().size();
		pp.getNative_id();
		
		ppmgr.addpp(pp);
		
		int n = ppmgr.showAllModifiedPackages().size();
		pp = ppmgr.showAllModifiedPackages().get(n - 1);
		int userid = Integer.parseInt(mappa.get("idUser")[0]);
		TravelList tl = new TravelList(1, userid , pp, 5);
		
		tl.toString();
		tl.getIdPackage().toString();
		
		tlmgr.add(tl);
		
		request.getRequestDispatcher("/UHome.jsp").forward(request, response); 
		//response.getOutputStream().println("<h1>TravelDream</h1><h2>" + mappa.get("name")[0]+ " succesfully registered!</h2>" + "| <a href=\"Index.jsp\"> Login </a> | Registration |");
		
	}

}
